package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.db.domain.Member;
import com.mall.db.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/system/lc")
public class LoginController {

    @Resource(name = "memberService")
    private MemberService ms;


    /**
     * 验证登录信息
     * */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(Member member, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","");
        map.put("is",false);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("login_name",member.getLoginName());
        Member user =  ms.getOne(qw);
        if (user == null){
            map.put("msg","用户不存在请重新登录");
        }else if (user != null){
            if (!user.getPassword().equals(member.getPassword())){
                map.put("msg","密码错误，请重新输入");
            }else {
                map.put("is",true);
                map.put("msg","登录成功");
                map.put("obj",user);
                session.setAttribute("user",user);
            }
        }
        return map;
    }

}
